package org.example.domain.strategy.model.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.example.types.common.Constants;
import org.example.types.exception.AppException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 策略规则实体
 */
@Data
@AllArgsConstructor
@Builder
@NoArgsConstructor
public class StrategyRuleEntity {
    /**
     * 策略ID
     */
    private Long strategyId;
    /**
     * 奖品ID
     */
    private Integer awardId;
    /**
     * 规则类型
     */
    private Integer ruleType;
    /**
     * 规则值
     */
    private String ruleValue;
    /**
     * 规则描述
     */
    private String ruleDesc;
    private String ruleModel;

    public Map<String, List<Integer>> getRuleWeightValues(){
        if (!"rule_weight".equals(ruleModel)) return null;
        String[] ruleValueGroups = ruleValue.split(Constants.SPACE);
        Map<String, List<Integer>> resultMap = new HashMap<>();
        for (String ruleValueGroup : ruleValueGroups){
            //检查输入是否为空
            if (ruleValueGroup == null || ruleValueGroup.isEmpty()){
                return resultMap;
            }
            //分割字符串以获取键和值
            String[] parts = ruleValueGroup.split(Constants.COLON);
            if (parts.length != 2){
                throw new AppException("rule_weight rule_rule invalid input format"+ruleValueGroup);
            }
            //解析值
            String[] valueStrings = parts[1].split(Constants.SPLIT);
            List<Integer> values = new ArrayList<>();
            for (String valueString : valueStrings){
                values.add(Integer.parseInt(valueString));
            }
            //将键和值放入Map中
            resultMap.put(ruleValueGroup,values);
        }
        return resultMap;
    }
}
